package com.cn.qgx428.lazydiary.ui.activity.subActivity

import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.cn.qgx428.lazydiary.Presenter.UserPresenter
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.SPUtil
import com.cn.qgx428.lazydiary.ui.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_pwd_reset.*
import org.jetbrains.anko.toast

class ResetPwdActivity : BaseActivity() {
    override fun initUI() {
        hindActionBar()
        setStatuBarColor(Color.BLACK)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pwd_reset)

        pwdReset_back.setBackOnClickListener { finish() }
        pwdReset_commit.setOnClickListener({
            resetPwd()
        })
    }

    fun resetPwd(){
        if (pwdReset_origin.getText().toString().length < 6){
            toast("原密码长度不对")
            return
        }
        if (pwdReset_new.getText().toString().length < 6){
            toast("新密码长度不对")
            return
        }
        UserPresenter.resetPwd(SPUtil.currntUserId().toInt(),
                pwdReset_origin.getText().toString(),
                pwdReset_new.getText().toString(),
                {
                    if (it.code.equals("-2")){
                        toast(it.msg)
                    } else{
                        toast(it.msg)
                        // 更新SP信息
                        SPUtil.get().edit().putString("pwd", pwdReset_new.getText().toString()).commit()
                        finish()
                    }
                })
    }
}
